//
//  NewCardVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/1/17.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "NewCardVc.h"
#import <UIImageView+WebCache.h>
#import "UIImage+Extension.h"
#import "ShareTool.h"
#import "MBProgressHUD+MJ.h"
#import "ConNewEditCardVc.h"
#import "QRCodeCreateTool.h"

@interface NewCardVc ()
@property (weak, nonatomic) IBOutlet UIView *shareView;
@property (weak, nonatomic) IBOutlet UIView *iconConView;

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UILabel *positionLab;

@property (weak, nonatomic) IBOutlet UILabel *iphoneLab;
@property (weak, nonatomic) IBOutlet UILabel *addressLab;
@property (weak, nonatomic) IBOutlet UILabel *tagLab;
@property (weak, nonatomic) IBOutlet UIImageView *codeView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *codeWCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconWCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgImgHCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *centerViewOffsetCons;
@property (weak, nonatomic) IBOutlet UILabel *shareLab;
@property (weak, nonatomic) IBOutlet UIButton *shareBtnOne;
@property (weak, nonatomic) IBOutlet UIButton *shareBtnTwo;
@property (weak, nonatomic) IBOutlet UIButton *shareBtnThree;


@end

@implementation NewCardVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"微名片";
    [self setNavi];
    [self setConfig];
}
- (void)setConfig
{
    self.view.backgroundColor = RGBColorOne(102);
    self.bgImgHCons.constant = (ScreenW-40)*130/279;
    self.centerViewOffsetCons.constant = 40;
    if (IS_IPHONE_5) {
        self.bgImgHCons.constant = (ScreenW-40)*100/279;
        self.codeWCons.constant = 40;
        self.iphoneLab.font = Font(15);
        self.addressLab.font = Font(15);
        self.tagLab.font = Font(14);
        self.iconWCons.constant = 70;
        self.iconView.layer.cornerRadius = 35;
        self.iconConView.layer.cornerRadius = 38;
        self.nameLab.font = Font(16);
        self.positionLab.font = Font(12);
        self.centerViewOffsetCons.constant = 20;
    }else if (IS_IPHONE_X) {
        self.iconWCons.constant = 110;
        self.iconView.layer.cornerRadius = 55;
        self.iconConView.layer.cornerRadius = 58;
        self.nameLab.font = Font(22);
        self.positionLab.font = Font(16);
    }
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:self.info.portraitUrl] placeholderImage:GetImage(@"club_icon")];
    self.nameLab.text = self.info.name;
    self.positionLab.text = self.info.position;
    self.iphoneLab.text = self.info.mobilePhone;
    self.addressLab.text = self.info.detailedAddress;
    self.tagLab.text = self.info.personalizedSignature;
    self.codeView.image = [QRCodeCreateTool createQRCodeWithString:self.info.shareUrl width:100];
    if (self.isOther) {
        self.shareLab.hidden = YES;
        self.shareBtnOne.hidden = YES;
        self.shareBtnTwo.hidden = YES;
        self.shareBtnThree.hidden = YES;
    }
}

- (void)setNavi
{
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20]}];
    self.navigationController.navigationBar.barTintColor = RGBColorOne(102);
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    if (!self.isOther) {
        UIButton *saveBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
        [saveBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        saveBtn.titleLabel.font = Font(16);
        [saveBtn addTarget:self action:@selector(editClick) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:saveBtn];
    }
}
- (void)editClick
{
    ConNewEditCardVc *editVc = [[ConNewEditCardVc alloc]init];
    editVc.info = self.info;
    editVc.isFromCard = YES;
    [self.navigationController pushViewController:editVc animated:YES];
}
- (UIBarButtonItem *)customBackItemWithTarget:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 40, 40);
    [button sizeToFit];
    [button addTarget:target
               action:action
     forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}
- (IBAction)shareClick:(UIButton *)btn
{
    UIImage *shareImg = [UIImage snapshotView:self.view fromRect:CGRectMake(self.shareView.x-15, self.shareView.y-15, self.shareView.width+30, self.shareView.height+30) withCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    if (btn.tag == 2) {
        [self loadImageFinished:shareImg];
    }else {
        [ShareTool shareWithImg:shareImg type:btn.tag];
    }
}

//保存图片到相册
- (void)loadImageFinished:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    [MBProgressHUD showOnlyText:@"保存图片成功,请去相册查看" view:self.view];
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}

@end
